/// beamnpc.txt
// A monster with a special ability scripted in. The ability is an beam, used 
// 50% of the time.

// Memory Cells:
//   Cell 0 - If 0, default behavior. If 1, it doesnt fidget.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.
//   Cell 4 - The ability to use.
//   Cell 5 - talking

// The abilities:
// 0 - Beam Slow
// 1 - Beam Curse
// 2 - Beam Fire (1-6 dam per level)
// 3 - Beam Acid
// 4 - Beam Terror
// 5 - Beam Daze
// 6 - Beam Debuff
// 7 - Beam Cold (1-8 dam per level)
// 8 - Beam Energy (1-10 dam per level)
// 9 - Beam Charm
// 10 - unused
// 11 - Beam lightning

begincreaturescript;

variables;

short i,target;
short last_abil;
short used_abil = 0;
short mental_effect = 0;

body;

beginstate INIT_STATE;
	last_abil = get_current_tick();

	if (get_memory_cell(4) == 4)	
		mental_effect = 1;
	if (get_memory_cell(4) == 5)	
		mental_effect = 1;
	if (get_memory_cell(4) == 9)	
		mental_effect = 1;

	break;

beginstate DEAD_STATE;
	if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0))
		inc_flag(get_memory_cell(1),get_memory_cell(2),1);
break;

beginstate START_STATE; 
	// if I have a target for some reason, go attack it
	if (target_ok()) {
		//if (dist_to_char(get_target()) <= 16)
			set_state(3);
		//	else set_foe_target(ME,-1);
		}
	
	if (get_foe_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	if (my_dist_from_start() >= 6) {
		if (get_ran(1,1,100) < 40) 
			return_to_start(ME,1);
		}
		else if (get_memory_cell(0) == 0)
			fidget(ME,25);

	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (target_ok() == FALSE)
		set_state(START_STATE);
	used_abil = 0;

	if ((get_ran(1,0,100) < 50) && (can_see_char(get_target())) && (is_combat()) && (tick_difference(last_abil,get_current_tick()) > 0)) {
		run_char_animation(2,1,35);	
		if (get_memory_cell(4) == 0) { // beam slow
			print_named_str(ME,"fires a beam of slowing energy.");
			set_char_status(get_target(),1,-8);
			//place_particle_num(get_target(),2,2,8);
			pc_heard_sound_delay(174,100);						
			shoot_projectile(ME,get_target(),34);
			used_abil = 1;
			}
		if (get_memory_cell(4) == 1) { // beam curse
			print_named_str(ME,"casts a powerful curse.");
			set_char_status(get_target(),0,-8);
			set_char_status(get_target(),8,-8);
			//place_particle_num(get_target(),3,2,8);
			shoot_projectile(ME,get_target(),38);
			pc_heard_sound_delay(128,100);						
			used_abil = 1;
			}
		if (get_memory_cell(4) == 2) { // beam fire
			print_named_str(ME,"summons a rain of fire.");
		  	place_particle_num(get_target(),2,4,8);
			damage_char(get_target(),get_ran(get_level(ME) + get_attack_bonus(ME),1,6),2);
			pc_heard_sound_delay(178,100);						
			shoot_projectile(ME,get_target(),31);
			used_abil = 1;
			}
		if ((get_memory_cell(4) == 3) || ((get_target() == pc_num()) && (mental_effect))) { // beam acid
			print_named_str(ME,"spits a spray of acid.");
			set_char_status(get_target(),6,get_ran(get_level(ME) + get_attack_bonus(ME),1,3));
			//place_particle_num(get_target(),7,2,8);
			pc_heard_sound_delay(177,100);						
			shoot_projectile(ME,get_target(),32);
			used_abil = 1;
			}
		if ((get_memory_cell(4) == 4) && (used_abil == 0)) { // beam terror
			print_named_str(ME,"radiates pure terror.");
			set_char_status(get_target(),7,get_level(ME) + 5 + get_attack_bonus(ME));
			//place_particle_num(get_target(),8,2,8);
			pc_heard_sound_delay(170,100);						
			used_abil = 1;
			}
		if ((get_memory_cell(4) == 5) && (used_abil == 0)) { // beam daze
			print_named_str(ME,"radiates confusing energy.");
			set_char_status(get_target(),17,get_level(ME) + 5 + get_attack_bonus(ME));
			//place_particle_num(get_target(),8,2,8);
			pc_heard_sound_delay(124,100);			
			used_abil = 1;
			}
		if (get_memory_cell(4) == 6) { // beam debuff
			print_named_str(ME,"surrounds a foe with static.");
			set_char_status(get_target(),9,-15);
			set_char_status(get_target(),11,-15);
			set_char_status(get_target(),12,-15);
			set_char_status(get_target(),13,-15);
			set_char_status(get_target(),14,-15);
			set_char_status(get_target(),15,-15);
			set_char_status(get_target(),16,-15);
			set_char_status(get_target(),18,-15);
			set_char_status(get_target(),19,-15);
			place_particle_num(get_target(),8,2,8);
			pc_heard_sound_delay(195,100);						
			used_abil = 1;
			}
		if (get_memory_cell(4) == 7) { // beam cold
		  	place_particle_num(get_target(),1,4,8);
			print_named_str(ME,"flings a spray of icy particles.");
			damage_char(get_target(),get_ran(get_level(ME),1,8),6);
			pc_heard_sound_delay(144,100);						
			shoot_projectile(ME,get_target(),41);
			used_abil = 1;
			}
		if (get_memory_cell(4) == 8) { // beam energy
			print_named_str(ME,"fires a beam of searing energy.");
		  	place_particle_num(get_target(),1,4,8);
			damage_char(get_target(),get_ran(get_level(ME) + get_attack_bonus(ME),1,10),1);
			pc_heard_sound_delay(179,100);						
			shoot_projectile(ME,get_target(),38);
			used_abil = 1;
			}
		if ((get_memory_cell(4) == 9) && (used_abil == 0)) { // beam charm
			print_named_str(ME,"emits a strange, wonderful scent.");
			set_char_status(get_target(),5,get_level(ME) + 5 + get_attack_bonus(ME));
			//place_particle_num(get_target(),8,2,8);
			pc_heard_sound_delay(100,100);						
			used_abil = 1;
			}
		//if (get_memory_cell(4) == 10) { // beam freeze
		//	print_named_str(ME,"fires a beam of ice.");
		//	set_char_status(get_target(),19,get_level(ME) + 5 + get_attack_bonus(ME));
		//	place_particle_num(get_target(),8,2,8);
		//	pc_heard_sound_delay(165,100);						
		//	shoot_projectile(ME,get_target(),41);
		//	used_abil = 1;
		//	}
		if (get_memory_cell(4) == 11) { // lightnign debuff
			print_named_str(ME,"launches a spray of lightning.");
			set_char_status(get_target(),20,10);
			damage_char(get_target(),get_ran(get_level(ME) + get_attack_bonus(ME),1,6),1);
		  	//place_particle_num(get_target(),1,4,8);
			pc_heard_sound_delay(241,100);					
			shoot_projectile(ME,get_target(),43);
			used_abil = 1;
			}

		last_abil = get_current_tick();
		end();
		}

	if (used_abil == 0)
		do_attack();
break;

beginstate TALKING_STATE;
	if (get_memory_cell(5) == 0) {
		print_str("Talking: It doesn't respond.");
		}
		else begin_talk_mode(get_memory_cell(5));
break;